package cn.slg.module.system.dal.dataobject.state.functional;

import cn.slg.module.system.dal.dataobject.wujiang.WuJiangDO;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.lang.reflect.Method;

@Data
@NoArgsConstructor
public class GuiBiState extends FunctionalState {

    // 不同来源可叠加（规避概率），是非线性叠加。
    // TODO 确定叠加算法
    boolean canAdd = true;

    // 相同战法来源（不可以叠加规避概率），
    // 但可刷新持续效果（如果新的状态持续效果和既有的剩余回合数取较大值）
    boolean canRefresh = true;

    // 规避
    String stateName = "规避";

    // 规避概率，
    Float rate = 0f;

    public GuiBiState(boolean isNegative, String stateName, WuJiangDO inflictor, WuJiangDO receivers, Method skill, String skillName, byte continueTimes, Float baseRate) {
        super(isNegative, stateName, inflictor, receivers, skill, skillName, continueTimes);
        this.rate = baseRate;
    }

    public static boolean canAddState() {
        return true;
    }


    public static void addState(boolean isNegative, String stateName, WuJiangDO inflictor, WuJiangDO receiver, Method skill, String skillName, byte continueTimes, Float baseRate) {
        boolean add = true;
        for (FunctionalState functionalState : receiver.getFunctionalStates()) {
            if (functionalState.getStateName().equals("规避")) {
                // 如果已经有规避了
                add = false;
                GuiBiState guiBiState = (GuiBiState) functionalState;
                if (guiBiState.getSkillName().equals(skillName)) {
                    // 如果是相同战法来源，只可刷新
                } else {
                    // 如果是不同战法来源，可叠加规避概率
                    // TODO 考虑非线性叠加算法
                    guiBiState.setRate(guiBiState.getRate() + baseRate);
                }
                // 只要是已经有规避了，都要刷新施加者、持续时间和剩余时间
                guiBiState.setInflictor(inflictor);
                guiBiState.setContinueTimes(continueTimes);
                guiBiState.setRemainTimes(continueTimes);
            }
        }
        if (add) {
            GuiBiState guiBiState = new GuiBiState(isNegative, stateName, inflictor, receiver, skill, skillName, continueTimes, baseRate);
            receiver.getFunctionalStates().add(guiBiState);
        }
    }
}
